<template>
  <div>
    <a-page-header title="修改基础信息" :show-back="false"></a-page-header>
    <ma-form
        ref="formRef"
        v-model="record"
        :columns="columns"
        class="mt-7"
        @submit="submit"
    >
      <template #formButtons v-if="  !auth('customers:customer:update')  ||  (record.auth_status == 4 && !auth('customers:customer:updateTrue')) ">
        <a-button @click="close()">取消</a-button>
      </template>
    </ma-form>
  </div>
</template>

<script setup>
import {reactive, ref, inject, watch} from 'vue'
import customer from '@/api/customers/customer'
import {Message} from '@arco-design/web-vue'
import { auth } from '@/utils/common'

const formRef = ref()
const record = inject('record')
const crudRef = inject('crudRef')
const close = inject('close')
const submit = async (data) => {
  if (data) {
    const response = await customer.update(record.value.id, data)
    if (!response.success) {
      return
    }
    record.value.password = null
    Message.success(response.message)
    formRef.value.reset()
    return true
  }
}

const columns = reactive([
  {
    title: "用户名",
    dataIndex: "username",
    formType: "input",
    required: true,
  },
  {
    title: "昵称",
    dataIndex: "nick",
    formType: "input",
    required: true,
  },
  {
    title: "密码",
    dataIndex: "password",
    formType: "input-password",
    type: "password"
  },
  {
    title: "标签",
    dataIndex: "label_code",
    formType: "hide",
  },
  {
    title: "结算方式",
    dataIndex: "pay_type",
    formType: "select",
    required: true,
    dict: {
      name: "settlement",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "账户状态",
    dataIndex: "status",
    formType: "select",
    required: true,
    dict: {
      name: "customerType",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "联系人",
    dataIndex: "contact",
    formType: "input",
    required: true,
  },
  {
    title: "邮箱",
    dataIndex: "email",
    formType: "input"
  },
  {
    title: "手机",
    dataIndex: "mobile",
    formType: "input",

  },
  {
    title: "电话",
    dataIndex: "tel",
    formType: "input"
  },
  {
    title: "QQ",
    dataIndex: "qq",
    formType: "input"
  },
  {
    title: "客户等级",
    dataIndex: "grade_id",
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      url: 'customers/customerGrade/list',
      // 指定组件接收的props
      props: {label: 'name', value: 'id'},
      translation: true,
      params: {
        select: 'id,name',
      }
    },
    formType: "select"
  },
  {
    title: "信用额度",
    dataIndex: "credit_limit",
    addDefaultValue: 0,
    formType: "input"
  },
  {
    title: "财务代表",
    dataIndex: "finance_id",
    formType: "select",

    isEcho: true,
    virtualListProps:{},
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      url: 'system/user/list',
      // 指定组件接收的props
      props: {label: 'nickname', value: 'id'},
      translation: true, 
      params: {
        select: 'id,username,nickname',
      }
    },

  },
  {
    title: "客服代表",
    dataIndex: "service_id",
    formType: "select",

    isEcho: true,
    virtualListProps:{},
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      url: 'system/user/list',
      // 指定组件接收的props
      props: {label: 'nickname', value: 'id'},
      translation: true,
      params: {
        select: 'id,username,nickname',
      }
    },

  },
  {
    title: "销售代表",
    dataIndex: "seller_id",
    formType: "select",
    virtualListProps:{},
    isEcho: true,
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      url: 'system/user/list',
      // 指定组件接收的props
      props: {label: 'nickname', value: 'id'},
      translation: true,
      params: {
        select: 'id,username,nickname',
      }
    },

  },
  {
    title: "销售来源",
    dataIndex: "source_id",
    translation: true,
    formType: "select",
    virtualListProps:{},
    isEcho: true,
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      url: 'system/user/list',
      // 指定组件接收的props
      props: {label: 'username', value: 'id'},
      translation: true,
      params: {
        select: 'id,username',
      }
    },
  },
  {
    title: "所属分公司",
    dataIndex: "organization_id",
    dict: {
      url: 'system/dept/list',
      props: {label: 'name', value: 'id'},
      translation: true,
      params: {
        select: 'id,name,parent_id',
        parent_id: 0
      }
    },

    formType: "select"
  },
  {
    title: "站点",
    dataIndex: "depots",
    formType: "select",
    dict: {
      url: 'warehouse/depot/list',
      props: {label: 'name', value: 'id'},
      translation: true,
      params: {
        select: 'id,name',
      }
    },
  },
  {
    title: "员工账号前缀",
    dataIndex: "staff",
    formType: "input",
    hide:true
  },
])
</script>